package xlj.lib.android.base.push;

import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

public class NotificationPoster {
    private static int notificationRequestId = 0;
    private static int notifyId;

    public static void setNotifyId(int notifyId) {
        NotificationPoster.notifyId = notifyId;
    }

    public static void post(Context context, PushData pushData) {
        NotificationManager mNotificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        NotificationCompat.Builder builder = notificationBuilder(context,PushSettings.pushSettings().getPushIcon(),
                pushData.getContent(),
                pushData.getTitle(),!PushSettings.pushSettings().isVibrateOff(),!PushSettings.pushSettings().isVoiceOff());
        Intent intent = new Intent(context, XLPushManager.getPushManager().getMainCls());
        intent.putExtra("pushData",pushData);
        PendingIntent contentIntent = PendingIntent.getActivity(context,
                notificationRequestId++, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        builder.setContentIntent(contentIntent);
        mNotificationManager.notify(notifyId, builder.build());

    }

    private static NotificationCompat.Builder notificationBuilder(Context context, int iconRes, String text, String title, boolean isVibrate, boolean isSound) {

        long when = System.currentTimeMillis();
        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "");
        builder.setSmallIcon(iconRes);
        builder.setTicker("您有新短消息，请注意查收！");
        builder.setWhen(when);
        builder.setContentTitle(title);
        builder.setContentText(text);
        if (isVibrate && !isSound) {
            builder.setDefaults(NotificationCompat.DEFAULT_VIBRATE);
        } else if (isVibrate && isSound) {
            builder.setDefaults(NotificationCompat.DEFAULT_SOUND | NotificationCompat.DEFAULT_VIBRATE);
        } else if (!isVibrate && isSound) {
            builder.setDefaults(NotificationCompat.DEFAULT_SOUND);
        }
        return builder;
    }

    public static void cancel(Context context, int notifyId) {
        NotificationManager mNotificationManager = (NotificationManager) context
                .getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.cancel(notifyId);
    }
}
